linux编程GCC、gdb、Makefile、make、

您所在的位置:网站首页 yacc command not found linux编程GCC、gdb、Makefile、make、

linux编程GCC、gdb、Makefile、make、

#linux编程GCC、gdb、Makefile、make、| 来源: 网络整理| 查看: 265

文章目录 GCCgcc编译过程gcc编译多文件 gdb动态库和静态库创建静态库使用静态库 动态库动态库的创建动态库的使用 静态库和动态库的区别 makemake是什么1 make命令和Makefile文件2 MakefiIe文件编写makefile抒写规则 makeMakefile文件中的变量Makefile通配符 automake

GCC

gcc是GNU Compiler Collection

gcc [选项] 参数 -o 指定目标文件的名称 -g 使生成的可执行文件中包含debug信息 -c 只编译不连接 -E 只做预处理 -S 由C翻译成汇编 ouc-13@ouc-13:~/hanfeng$ vim main.c #include int main(){ printf("hello first\n"); return 0; } ouc-13@ouc-13:~/hanfeng$ gcc main.c #生成了个a.out # 执行a.out ouc-13@ouc-13:~/hanfeng$ ./a.out hello first ouc-13@ouc-13:~/hanfeng$ gcc -o first main.c #生成了个first gcc编译过程 ouc-13@ouc-13:~/hanfeng$ vim main.c #include #define PI 3.14 int main(){ printf("hello\n"); return 0; } # ===========预编译========得到.i文件=== ouc-13@ouc-13:~/hanfeng$ gcc main.c -E -o main.i ouc-13@ouc-13:~/hanfeng$ cat main.i | tail -15 extern void funlockfile (FILE *__stream) __attribute__ ((__nothrow__ , __leaf__)); # 864 "/usr/include/stdio.h" 3 4 extern int __uflow (FILE *); extern int __overflow (FILE *, int); # 879 "/usr/include/stdio.h" 3 4 # 2 "main.c" 2 # 3 "main.c" int main(){ printf("hello2\n"); return 0; } # ===========编译=============得到.s文件=== ouc-13@ouc-13:~/hanfeng$ gcc main.i -S ouc-13@ouc-13:~/hanfeng$ cat main.s | head -15 .file "main.c" .text .section .rodata .LC0: .string "hello" .text .globl main .type main, @function main: .LFB0: .cfi_startproc pushq %rbp .cfi_def_cfa_offset 16 .cfi_offset 6, -16 movq %rsp, %rbp # ==========汇编============得到main.o二进制文件===== ouc-13@ouc-13:~/hanfeng$ gcc main.s -c # ==========链接============默认得到a.out可执行文件===可以通过-o指定文件名 ouc-13@ouc-13:~/hanfeng$ gcc main.o -o second ouc-13@ouc-13:~/hanfeng$ ./second hello2 gcc编译多文件 ouc-13@ouc-13:~/hanfeng$ vim multi.c #include #include "bank.h" int main(){ int a=5,b=18,c; c=max(a,b); printf("a与b的最大值为%d\n",c); } ouc-13@ouc-13:~/hanfeng$ vim m.c int max(int a,int b){ return a>b?a:b; } ouc-13@ouc-13:~/hanfeng$ vim bank.h int max(int,int); # 方法1=======多个文件一起编译===实际上上多个源文件分别编译后再链接成test可执行文件 ouc-13@ouc-13:~/hanfeng$ gcc multi.c m.c -o test ouc-13@ouc-13:~/hanfeng$ ./test a与b的最大值为18 # 方法2====分别编译=== gcc -c multi.c # 编译得到multi.o gcc -c m.c # 编译得到m.o gcc multi.o m.o -o test # multi.o 和m.o 链接成可执行文件test struct student{ int id; char name[20]; float score; };//定义了一个结构体类型,类型名为struct student typedef struct student{ int id; char name[20]; float score; }STU;//定义了一个结构体类型,类型名为STU 用包含的,编译器会从系统目录中寻找头文件,系统目录指的是: /usr/lib/gcci686-linux-gnu/4.6/include /usr/local/include /usr/include/i386-linux-gnu/ /usr/include/ 用双引号""直接引起来的头文件和源文件在同一个目录下。这样,编译器就会先在该目录(当前工作目录)下搜索,如果找不到再去系统目录下搜索。 可以写绝对路径,也可以在编译时指定相对路径 gcc -I 相对路径 也可以将.h文件放到系统目录下,但不推荐

头文件重复包含

如果A.h中包含头文件C.h,B.h也包含了C.h,而源文件中包含了A.h和B.h。

编译时机会报错

如果头文件中重复包含了一些函数的生, 那么在编译时不会出现错误

避免头文件重复包含的解决方法就是在头文件中使用条件编译进行控制:

#ifndef _MY_H//_MY_H_ 通常用大写,并且加上适当的下划线; #define _MY_H_ #endif gdb ouc-13@ouc-13:~/hanfeng$ vim gdbtest.c #include int add(int start,int end){ int i,sum; for(i=start;i


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3